app.controller('acaController', function($scope, $http, $q, $state, $ionicHistory, $ionicPopover, $ionicModal, $location, $stateParams, userService, payService, $ionicPopup, $httpParamSerializer) {
  $ionicModal.fromTemplateUrl('templates/touzijinguize.html', {  
    scope: $scope,
    animation: 'slide-in-up'
  }).then(function(modal) {  
    $scope.modaltouzijinguize = modal;  
  });
  $scope.showtouzijinguize=function(){    
    $scope.modaltouzijinguize.show();
  };
  $scope.closetouzijinguize=function(){    
    $scope.modaltouzijinguize.hide();
  };  
  $scope.$on('$destroy', function() {
    $scope.modaltouzijinguize.remove();//对应删除      
  });
})
.controller('acbController', function($scope, $http, $q, $state, $ionicHistory, $ionicPopover, $ionicModal, $location, $stateParams, userService, payService, $ionicPopup, $httpParamSerializer) {
 
})
.controller('accController', function($scope, $http, $q, $state, $ionicHistory, $ionicPopover, $ionicModal, $location, $stateParams, userService, payService, $ionicPopup, $httpParamSerializer) {
 
})
.controller('acdController', function($scope, $http, $q, $state, $ionicHistory, $ionicPopover, $ionicModal, $location, $stateParams, userService, payService, $ionicPopup, $httpParamSerializer) {
 
})
.controller('aceController', function($scope, $http, $q, $state, $ionicHistory, $ionicPopover, $ionicActionSheet, $ionicModal, $location, $stateParams, userService, payService, $ionicPopup, $httpParamSerializer) {
  $ionicModal.fromTemplateUrl('templates/shareguize.html', {  
    scope: $scope,
    animation: 'slide-in-up'
  }).then(function(modal) {  
    $scope.modalshareguize = modal;  
  });
  $scope.showshareguize=function(){    
    $scope.modalshareguize.show();
  };
  $scope.closeshareguize=function(){    
    $scope.modalshareguize.hide();
  };  
  $scope.$on('$destroy', function() {
    $scope.modalshareguize.remove();//对应删除 
  });
  //modal先后顺序的问题?todo  键盘输入?todo
  $ionicModal.fromTemplateUrl('templates/activity/ace_share.html', {  
    scope: $scope,
    animation: 'slide-in-up'
  }).then(function(modal) {  
    $scope.modalaceshare = modal;  
  });
  $scope.share = function() {
    $scope.modalaceshare.show(); 
    var emodal = document.getElementsByClassName('share')[0];
    angular.element(emodal.parentNode.parentNode).addClass('sharemodal');
    var ebackdrop = document.getElementsByClassName('backdrop')[0];
    angular.element(ebackdrop).addClass('visible active');
  }
  
  var closeshare = function(){ 
    $scope.modalaceshare.hide();
    var ebackdrop = document.getElementsByClassName('backdrop')[0];
    angular.element(ebackdrop).removeClass('visible active');
  };

  $scope.cancelshare = function(){ 
    closeshare();
  };
  $scope.$on('$destroy', function() {
    $scope.modalaceshare.remove();//对应删除     
    var ebackdrop = document.getElementsByClassName('backdrop')[0];
    angular.element(ebackdrop).removeClass('visible active');    
  });


  var toshare = window.localStorage.loginx;
  if (toshare == "toshare"){
    window.localStorage.removeItem('loginx');
  } 
  
/*
  YCQQ.checkClientInstalled(function(){
    console.log('client is installed');
  },function(){
    // if installed QQ Client version is not supported sso,also will get this error
    console.log('client is not installed');
  });  */
  //分享完 关闭modal 在弹出提示
  $scope.shareqq = function() {
    var args = {};
    args.url = "http://www.tr008.com/acte.html?inviteCode="+window.localStorage.memberId;
    args.title = "免费领取100元红包";
    args.description = "理财咨询、理财规划、理财产品，法律咨询。开启您的安全理财之旅。";
    args.imageUrl = "http://www.tr008.com/images/tr108.png";
    args.appName = "金法宝";
    YCQQ.shareToQQ(function(){
      console.log("share success");
      $ionicPopup.alert({
        title: '分享成功',
        template: '感谢您的支持！',
        okText: '关闭'
      }).then(function(){
        closeshare();
      });
    },function(failReason){
      console.log(failReason);
      $ionicPopup.alert({
        title: '分享失败',
        template: '失败原因：' + failReason + '。',
        okText: '关闭'
      }).then(function(){
        closeshare();
      });
    },args);
  }
  $scope.shareqqzone = function() {
    var args = {};
    args.url = "http://www.tr008.com/acte.html?inviteCode="+window.localStorage.memberId;
    args.title = "免费领取100元红包";
    args.description = "理财咨询、理财规划、理财产品，法律咨询。开启您的安全理财之旅。";
    var imgs =['http://www.tr008.com/images/tr108.png',
    'http://www.tr008.com/images/tr108.png',
    'http://www.tr008.com/images/tr108.png'];
    args.imageUrl = imgs;
    YCQQ.shareToQzone(function () {
      console.log("share success");
      $ionicPopup.alert({
        title: '分享成功',
        template: '感谢您的支持！',
        okText: '关闭'
      }).then(function(){
        closeshare();          
        //var ebackdrop = document.getElementsByClassName('backdrop')[0];
        //angular.element(ebackdrop).addClass('visible active');
      });
    }, function (failReason) {
      console.log(failReason);
      $ionicPopup.alert({
        title: '分享失败',
        template: '失败原因：' + failReason + '。',
        okText: '关闭'
      }).then(function(){
        closeshare();
      });
    }, args);
  }

  $scope.shareweibo = function() {
    /*    
    YCWeibo.checkClientInstalled(function(){
      console.log('client is installed');
    },function(){
      console.log('client is not installed');
    });*/
    var args = {};
    args.url = "http://www.tr008.com/acte.html?inviteCode="+window.localStorage.memberId;
    args.title = "免费领取100元红包";
    args.description = "理财咨询、理财规划、理财产品，法律咨询。开启您的安全理财之旅。";
    args.imageUrl = "http://www.tr008.com/images/tr108.png";//if you don't have imageUrl,for android http://www.sinaimg.cn/blog/developer/wiki/LOGO_64x64.png will be the defualt one
    args.defaultText = "";
    YCWeibo.shareToWeibo(function () {
      console.log("share success");
      $ionicPopup.alert({
        title: '分享成功',
        template: '感谢您的支持！',
        okText: '关闭'
      }).then(function(){
        closeshare();
      });
    }, function (failReason) {
      console.log(failReason);
      $ionicPopup.alert({
          title: '分享失败',
          template: '失败原因：' + failReason + '。',
          okText: '关闭'
        }).then(function(){
          closeshare();
        });
    }, args);
  }


  //分享完 关闭modal 在弹出提示
  $scope.shareweixin = function() { 
      /*WeChat
      .share('文本', WeChat.Scene.session, function () {
          console.log('分享成功~');
      }, function (reason) {
          console.log(reason);
      });
      WeChat.share({
          type: WeChat.ShareType.webpage,
          title: "免费领取投资金1000元",
          description: "现在注册立得投资金1000元，畅享安全便捷理财，立即行动吧！",
          url: "http://www.tr008.com/acte.html?inviteCode="+window.localStorage.memberId
      }, WeChat.Scene.timeline, function () {
        closeshare();
        $ionicPopup.alert({
          title: '分享成功',
          template: '感谢您的支持！',
          okText: '关闭'
        });
        console.log('分享成功~');
      }, function (reason) {
        // 分享失败 
        closeshare();
        $ionicPopup.alert({
          title: '分享失败',
          template: '失败原因：' + reason + '。',
          okText: '关闭'
        });
        console.log(reason);
      });    
    Wechat.isInstalled(function (installed) {
      $ionicPopup.alert({
        title: 'Wechat', template: "Wechat installed: " + (installed ? "Yes" : "No"), okText: '关闭'
      });
      //alert("Wechat installed: " + (installed ? "Yes" : "No"));
    }, function (reason) {
      $ionicPopup.alert({
        title: 'Wechat',template: "Failed: " + reason, okText: '关闭'
      });
      //alert("Failed: " + reason);
    });*/
  //登录才能分享 带id
    if(window.localStorage.isLogin == 'true'){      
      Wechat.share({
        //text: "This is just a plain string",
        message: {    
          title: "免费领取100元红包",
          description: "理财咨询、理财规划、理财产品，法律咨询。开启您的安全理财之旅。",
          //url: url ? url : "http://www.tr008.com",
          mediaTagName: "分享",//(optional)
          thumb: "http://www.tr008.com/images/tr108.png",
          media: {
              type: Wechat.Type.WEBPAGE,  //Wechat.Type.LINK
              webpageUrl: "http://www.tr008.com/acte.html?inviteCode="+window.localStorage.memberId
          }
        },
        scene: Wechat.Scene.SESSION //  Wechat.Scene.TIMELINE //分享给微信好友 share weixin
      }, function () {
        //closeshare();
        $ionicPopup.alert({
          title: '分享成功',
          template: '感谢您的支持！',
          okText: '关闭'
        }).then(function(){
          closeshare();          
          //var ebackdrop = document.getElementsByClassName('backdrop')[0];
          //angular.element(ebackdrop).addClass('visible active');
        });
      }, function (reason) {        
        //closeshare();
        $ionicPopup.alert({
          title: '分享失败',
          template: '失败原因：' + reason + '。',
          okText: '关闭'
        }).then(function(){
          closeshare();          
          //var ebackdrop = document.getElementsByClassName('backdrop')[0];
          //angular.element(ebackdrop).addClass('visible active');
        });
      });
    }else{      
      window.localStorage.loginx = 'toshare';
      $state.go('counsel-login',{p:'toshare'});
    }
  }; 
  $scope.shareweixinpengyou = function() {
    //登录才能分享 带id
    if(window.localStorage.isLogin == 'true'){      
      Wechat.share({
        message: {    
          title: "免费领取100元红包",
          description: "理财咨询、理财规划、理财产品，法律咨询。开启您的安全理财之旅。",
          //url: url ? url : "http://www.tr008.com",您有100元红包可免费领取
          mediaTagName: "分享",//(optional)
          thumb: "http://www.tr008.com/images/tr108.png",
          media: {
              type: Wechat.Type.WEBPAGE,  //Wechat.Type.LINK
              webpageUrl: "http://www.tr008.com/acte.html?inviteCode="+window.localStorage.memberId
          }
        },
        scene: Wechat.Scene.TIMELINE   // Wechat.Scene.TIMELINE//分享至微信朋友圈 Wechat.Scene.TIMELINE    
      }, function () {
        //closeshare();
        $ionicPopup.alert({
          title: '分享成功',
          template: '感谢您的支持！',
          okText: '关闭'
        }).then(function(){
          closeshare();          
          //var ebackdrop = document.getElementsByClassName('backdrop')[0];
          //angular.element(ebackdrop).addClass('visible active');
        });
      }, function (reason) {        
        //closeshare();
        $ionicPopup.alert({
          title: '分享失败',
          template: '失败原因：' + reason + '。',
          okText: '关闭'
        }).then(function(){
          closeshare();          
          //var ebackdrop = document.getElementsByClassName('backdrop')[0];
          //angular.element(ebackdrop).addClass('visible active');
        });
      });
    }else{      
      window.localStorage.loginx = 'toshare';
      $state.go('counsel-login',{p:'toshare'});
    }    
  }; 

})

.controller('ActivityController', function($scope,$state,$ionicViewSwitcher,$location) {
  $scope.aca = function(){
    $ionicViewSwitcher.nextDirection('forward');
    $state.go('activity-aca',{}); 
  };    
  $scope.toreg = function(){
    $ionicViewSwitcher.nextDirection('forward');
    $state.go('user-register',{}); 
  };
  $scope.acb = function(){ 
    $ionicViewSwitcher.nextDirection('forward');
    $state.go('activity-acb',{}); 
  };  
  $scope.acc = function(){ 
    $ionicViewSwitcher.nextDirection('forward');
    $state.go('activity-acc',{}); 
  };  
  $scope.acd = function(){ 
    $ionicViewSwitcher.nextDirection('forward');
    $state.go('activity-acd',{}); 
  };  
  $scope.ace = function(){ 
    $ionicViewSwitcher.nextDirection('forward');
    $state.go('activity-ace',{}); 
  };  


  $scope.acf = function(){    
    inAppBrowserRef = cordova.InAppBrowser.open('',
      '_blank', 'location=no');
    // open win and turn off location 
    inAppBrowserRef = cordova.InAppBrowser.open('http://www.baidu.com/', 
      '_blank', 'location=no');  
      //url(encodeURI) target  options toolbar  android , closebuttoncaption=back 
    // attach listener to loadstart
    inAppBrowserRef.addEventListener('loadstart', function(event) { 
        var urlSuccessPage = "http://www.baidu.com";
        if (event.url == urlSuccessPage) {
        ref.close();    
        }
    });
  };

})
